<script type="text/javascript">
$(function(){
	
	//删除项目过程文件
	$(".btn-delete").ajaxModal({"title_text":"确认提示框","body_text":"您确定要删除吗？","url":"__APP__/Project/project_files_status","data":{"id":"mid","action":"action"}});

	KindEditor.ready(function(K){
		//上传附件
		var editorFile = K.editor();
		$('.file-submit').on('click',function(){
			var parents = $(this).parents(".input-group");
			editorFile.loadPlugin('insertfile',function(){
				document.cookie="file_name=project_files";
				document.cookie="file_type=ppt,pptx,pdf,doc,docx,jpg,jpeg,png,xls,xlsx,txt,rar,zip";
				editorFile.plugin.fileDialog({
					clickFn:function(url,title){
						parents.nextAll(".error-item").hide();
						parents.find(".file-name").val(title).prop("readonly",false);
						parents.nextAll(".file-url").val(url);
						parents.nextAll(".file-delete").show();
						editorFile.hideDialog();
					}
				});
			});
		});
	});
	
	//删除附件
	$(".file-delete").click(function(){
		$(this).hide();
		$(this).nextAll(".error-item").hide();
		$(this).nextAll(".file-url").val("");
		$(this).prevAll(".input-group").find(".file-name").val("").prop("readonly",true);
		$(this).parents("tr").find(".required-text2").next(".error-item").hide();
	});

	//添加
	$(".btn-add").on("click",function(){
		var modal = $("#edit-modal");
		modal.find(".modal-header h4").text("添加跟进记录");
		modal.find(".file-name").prop("readonly",true);
		modal.find(".file-name,.file-url,.remark").val("");
		var date = '{$today}';
		modal.find(".visit_date").val(date),
		modal.find(".file-delete").hide();
		modal.modal();
	});

	//编辑点击获取值
	$(".btn-edit").on("click",function(){
		var mid = $(this).attr("mid"),
		modal = $("#edit-modal");
		modal.find(".modal-header h4").text("编辑跟进记录");
		modal.find(".btn-affirm").attr({"mid":mid});
		$.ajax({
			type:"POST",
			async:false,
			dataType:"json",
			data:{"id":mid},
			url:"__APP__/Crm/getInfoByRecordId",
			success:function(data){
				modal.find(".project").val(data.project_id).select2();
				modal.find(".project").attr("disabled","disabled");
				$("#project_contact").html(data.contact_info).select2();
				modal.find(".follow_type").val(data.follow_type).select2();
				modal.find(".status").val(data.status).select2();
				modal.find(".status").attr("disabled","disabled");
				modal.find(".file-url").val(data.file_url);
				var s = data.work_type;
				if(s!=null){
					for(var i=0;i<4;i++){
						if(s.indexOf(i+1)!=-1){
							modal.find('.checkbox-item input[name="work_type[]"]').eq(i).attr("checked","checked");
						}else{
							modal.find('.checkbox-item input[name="work_type[]"]').eq(i).removeAttr("checked");
						}
					}
				}
				var wc = data.work_chance;
				if(wc!=null){
					for(var j=0;j<4;j++){
						if(wc.indexOf(j+1)!=-1){
							modal.find('.radio-item input[name="work_chance"]').eq(j).click();
						}else{
							modal.find('.radio-item input[name="work_chance"]').eq(j).is(':checked',false);
						}
					}
				}
				modal.find(".file-name").val(data.file_name);
				modal.find(".visit_date").val(data.visit_date);
				modal.find(".remark").val(data.remark);
				if(data.file_url){
					modal.find(".file-name").prop("readonly",false);
					modal.find(".file-delete").show();
				}else{
					modal.find(".file-name").prop("readonly",true);
					modal.find(".file-delete").hide();
				}
				modal.modal();
			}
		});
	});

	//项目联动联系人
	$("#main_project").change(function(){
		var val = $(this).val();
		$.ajax({
			type:"POST",
			async:false,
			dataType:"json",
			data:{"id":val},
			url:"__APP__/Crm/project_follow_ajax",
			success:function(re){
				var list = re.list;
				if(list){
					var html = "<option value='-10'>请选择</option>";
					for(var i=0;i<list.length;i++){
						html+= "<option value='"+list[i].contact_id+"'>"+list[i].username+"</option>"
					}
					$("#project_contact").html(html).select2();
				}
				if(re.stage==0){
					$(".status").val(0).select2();
				}else{
					$(".status").val(re.stage).select2();
				}
			}
		})
	});
	
	//确定提交
	$("#edit-modal").find(".btn-affirm").on("click",function(){
		var tag = true,
		mid = $(this).attr("mid"),
		action,
		modal = $("#edit-modal");
		if(!mid){
			mid = "";
			action="add";
		}else{
			action="edit";
		}
		//必填判断
		$('.required-text').each(function () {
			var val = $(this).val();
			if (val == '') {
				$(this).next('.error-item').show();
				tag = false;
			}
		})
		//必选判断
		$('.required_select').each(function () {
			var val = $(this).val();
			if (val == 0 || val == -10) {
				$(this).siblings('.error-item').show();
				tag = false;
			}
		})
		//单选必选判断
		$(".radio-item").each(function(){
			var $val = $("[name='work_chance']:checked").length;
			if($val==0){
				$(".radio-item").find(".error-item").show();
				tag = false;
			}
		});
		
		var checks = $("input:checkbox[name='work_type[]']:checked").map(function(index,elem) {
			return $(elem).val();
		}).get().join(',');

		if(checks==""){
			$(".checkbox-item").find(".error-item").show();
			tag = false;
		}
		
		if(tag){
			var project = modal.find(".project").val(),
			contact = modal.find(".contact").val(),
			ft = modal.find(".follow_type").val(),
			st = modal.find(".status").val(),
			wc = $('.radio-item input[name="work_chance"]:checked').val(),
			file_name = modal.find(".file-name").val(),
			file_url = modal.find(".file-url").val(),
			visit_date = modal.find(".visit_date").val(),
			remark = modal.find(".remark").val();
			modal.hide();
			$.ajax({
				type:"POST",
				async:false,
				dataType:"json",
				data:{"action":action,"id":mid,"project_id":project,"contact_id":contact,"visit_date":visit_date,"remark":remark,"follow_type":ft,"status":st,"work_chance":wc,"work_type":checks,"file_name":file_name,"file_url":file_url},
				url:"__APP__/Crm/follow_ajax",
				success:function(data){
					success_pop(data.status);
				}
			});
		}
	});
	/*取消错误提示*/
	$('.required_select').change(function () {
		$(this).siblings('.error-item').hide();
	});
	$(".required-text").focus(function () {
		$(this).nextAll('.error-item').hide();
	});
	$(".radio-item").on('click',"label",function(){
		$(this).parents(".radio-item").find(".error-item").hide();
	});
	$(".checkbox-item").on('click',"label",function(){
		$(this).parents(".checkbox-item").find(".error-item").hide();
	});

	//取消
	$("#edit-modal").on('hidden.bs.modal',function(){
		$(this).find(".error-item").hide();
		$(this).find(".file-name,.file-url,.remark").val("");
		$(this).find("select").val(-10).select2();
		$(this).find(".btn-affirm").removeAttr("mid");
		window.location.reload();
	});
});
</script>